#include "baseapplication.h"
#include "texturesdl.h"

using namespace EPOC;

BaseApplication::BaseApplication()
{
	SetClassID(kCLASSID_APPLICATION);

	m_pGameManager = NULL;
	m_pGraphicsManager = NULL;
	m_pAudioManager = NULL;
	m_pInputManager = NULL;
}

BaseApplication::~BaseApplication()
{
	BaseApplication::Cleanup();
}


void BaseApplication::Cleanup()
{
	SAFE_RELEASE(m_pGameManager);
	SAFE_RELEASE(m_pInputManager);
	SAFE_RELEASE(m_pAudioManager);
	SAFE_RELEASE(m_pGraphicsManager);
}


bool BaseApplication::Initialize()
{
	if( CreateGraphicsManager() == false)
		return false;

	if(m_bAudioSupport )
	{
		if( CreateAudioManager() == false)
		{
			fprintf( stderr, "Application: Error, audio disabled\n");
			m_bAudioSupport = false;//return false;
		}
	}

	if( CreateInputManager() == false)
		return false;

	if( CreateGameManager() == false)
		return false;

	return true;
}


bool BaseApplication::CreateGraphicsManager()
{
	m_pGraphicsManager = new GraphicsManager(this);
	if(!m_pGraphicsManager || !m_pGraphicsManager->Initialize())
	{
		m_pGraphicsManager->Release();
		m_pGraphicsManager = NULL;

		return false;
	}

	return true;
}

bool BaseApplication::CreateAudioManager()
{
	m_pAudioManager = new AudioManager(this);
	if(!m_pAudioManager || !m_pAudioManager->Initialize(32, 44100, 2048))
	{
		m_pAudioManager->Release();
		m_pAudioManager = NULL;

		return false;
	}

	return true;
}

bool BaseApplication::CreateInputManager()
{
	m_pInputManager = new InputManager(this);
	if(!m_pInputManager || !m_pInputManager->Initialize())
	{
		m_pInputManager->Release();
		m_pInputManager = NULL;

		return false;
	}

	return true;
}

bool BaseApplication::CreateGameManager()
{
	m_pGameManager = new GameManager(this);
	if(!m_pGameManager || !m_pGameManager->Initialize())
	{
		m_pGameManager->Release();
		m_pGameManager = NULL;

		return false;
	}

	return true;
}


bool BaseApplication::Update(float fTimeDelta)
{
	if(m_pGameManager)
	{
		if(m_pGameManager->Update(fTimeDelta) == false)
		{
			m_pGameManager->Release();
			m_pGameManager = NULL;

			return false;
		}
	}

	if(m_pGraphicsManager)
	{
		if(m_pGraphicsManager->Update(fTimeDelta) == false)
		{
			m_pGraphicsManager->Release();
			m_pGraphicsManager = NULL;

			return false;
		}
	}

	if(m_pAudioManager)
	{
		if(m_pAudioManager->Update(fTimeDelta) == false)
		{
			m_pAudioManager->Release();
			m_pAudioManager = NULL;

			return false;
		}
	}

	if(m_pInputManager)
	{
		if(m_pInputManager->Update(fTimeDelta) == false)
		{
			m_pInputManager->Release();
			m_pInputManager = NULL;

			return false;
		}
	}

	return true;
}

void BaseApplication::Render(SDL_Renderer* pRenderer)
{
	if(m_pGraphicsManager)
		m_pGraphicsManager->Render(pRenderer);
}


void BaseApplication::HandleEvent(SDL_Event *pEvent)
{
	if(m_pInputManager)
		m_pInputManager->HandleEvent(pEvent);
}